// $Rev: 130 $

#ifndef CONNECTION_H
#define CONNECTION_H

// Mini-wrapper para sockets
// Las implementaciones son cortas, pero tal vez deberian ir en un ".cpp".

#include <poll.h>
#include <unistd.h> // para close
#include <sys/socket.h>

class Connection {
	public:
		Connection(int socket, pollfd *poll_info) :
			socket(socket), poll_info(poll_info), dead(false) {
			this->poll_info->fd = socket;
			this->poll_info->events = 0;
			this->poll_info->revents = 0;
		};
		bool is_dead() const {return dead;}
		void set_poll_info(pollfd *poll_info) {this->poll_info = poll_info;}
		virtual void process_events() = 0;
		virtual ~Connection() {if (socket >= 0) close(socket);}
	protected:
		ssize_t receive(void *buf, size_t len) {
			return ::recv(socket, buf, len, 0);
		}
		ssize_t send(const void *buf, size_t len) {
			return ::send(socket, buf, len, 0);
		}
		bool can_receive() {return poll_info->revents & POLLIN;}
		bool can_send() {return poll_info->revents & POLLOUT;}
		void filter_events(int flags) {poll_info->events = flags;}
		void kill_connection() {dead = true;}
	private:
		int socket;
		pollfd *poll_info;
		bool dead;
		// No permitimos operadores de copia o asignacion para esta clase.
		Connection(const Connection &ic);
		Connection &operator=(const Connection &ic);
};

#endif

